package HTML::FormFu::ConfigGenerator;
use Moose;
use DBIx::Class;
use YAML;
use List::Util 'first';

use version; our $VERSION = qv('0.0.1');

has 'schema' => (
    is  => 'rw',
    isa => 'DBIx::Class::Schema',
);

sub config_for {
    my ( $self, $rsname ) = @_;
    my $config = get_formfu_config( $rsname, $self->schema );
    Dump( $config );
}

sub _strip_class {
    my $fullclass = shift;
    my @parts     = split /::/, $fullclass;
    my $class     = pop @parts;
    return $class;
}

sub get_formfu_config {
    my( $class, $schema ) = @_;
    my %config;
    push @{$config{elements}}, get_elements ( $class, $schema, 0 );
    push @{$config{elements}}, {
        type => 'Submit',
        name => 'foo',
    };
    push @{$config{elements}}, {
        type => 'Hidden',
        name => $class. '_indicator',
        default => 1,
    };
    $config{id} = $class;
    $config{auto_id} = '%n';
    $config{element_defaults} = {
        radiogroup => { auto_id => '%n_%c' }
    };
    $config{auto_fieldset} = { legend => 'A fieldset' };
    return \%config;
}

sub get_elements {
    my( $class, $schema, $level, @exclude ) = @_;
    my $source = $schema->source( $class );
    my %primary_columns = map {$_ => 1} $source->primary_columns;
    my @elements;
    my @fieldsets;
    for my $col ( $source->columns ) {
        next if grep { $_ eq $col } @exclude;
        my $new_element = { name => $col };
        if( $primary_columns{$col} ){
            $new_element->{type} = 'Hidden';
        }   
        else{
            $new_element->{type}  = 'Text';
            $new_element->{label} = $col,
        }
        push @elements, $new_element;
    }
    for my $rel( $source->relationships ) {
        next if grep { $_ eq $rel } @exclude;
        my $info = $source->relationship_info($rel);
        my $rel_class = _strip_class( $info->{class} );
        my $elem_conf;
        if ( ! ( $info->{attrs}{accessor} eq 'multi' ) ) {
            push @elements, {
                type => 'Select',
                name => $rel,
                label => $rel,
                model_config => {
                    DBIC => { resultset => $rel_class },
                }
            };
        }
        elsif( $level < 1 ) {
            push @elements, { type => 'Hidden', name => 'count_' . $rel };
            push @elements, { type => 'Fieldset', name => $rel };
            my @new_exclude = get_foreign_cols ( $info->{cond} );
            my @new_elements = get_elements ( $rel_class, $schema, $level + 1, @new_exclude );
            my $new_check_field = first { $_->{type} ne 'Hidden' } @new_elements;
            push @elements, {
                type => 'Repeatable',
                nested_name => $rel,
                counter_name => 'count_' . $rel,
                model_config => { DBIC => { new_empty_row =>  $new_check_field->{name} } },
                elements => [
                @new_elements,
                {
                    type => 'Checkbox', name => 'delete_box', label => 'delete', value => 1,
                    model_config => { DBIC => { delete_if_true => 'delete_box' } } }
                ],
            }
        }
    }
    return @elements;
}

sub get_foreign_cols{
    my $cond = shift;
    my @cols;
    if ( ref $cond eq 'ARRAY' ){
        for my $c1 ( @$cond ){
            push @cols, get_foreign_cols( $c1 );
        }
    }
    elsif ( ref $cond eq 'HASH' ){ 
        for my $key ( keys %{$cond} ){
            if( $key =~ /foreign\.(.*)/ ){
                push @cols, $1;
            }
        }
    }
    return @cols;
}


#################### main pod documentation begin ###################
## Below is the stub of documentation for your module. 
## You better edit it!


=head1 NAME

HTML::FormFu::ConfigGenerator - Module abstract (<= 44 characters) goes here

=head1 SYNOPSIS

  use HTML::FormFu::ConfigGenerator;
  blah blah blah


=head1 DESCRIPTION

Stub documentation for this module was created by ExtUtils::ModuleMaker.
It looks like the author of the extension was negligent enough
to leave the stub unedited.

Blah blah blah.


=head1 USAGE



=head1 BUGS



=head1 SUPPORT



=head1 AUTHOR

    HASH(0x8152c28)
    CPAN ID: MODAUTHOR
    XYZ Corp.
    a.u.thor@a.galaxy.far.far.away
    http://a.galaxy.far.far.away/modules

=head1 COPYRIGHT

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.


=head1 SEE ALSO

perl(1).

=cut

#################### main pod documentation end ###################


1;
# The preceding line will help the module return a true value

